/************************************************************************/
/* Server / Client														*/
/************************************************************************/

/* Ensure a team is valid */
team.Valid = function( teamObj )

	/* If nothing, return false */
	if not teamObj then return false end
	
	/* Check all teams */
	for id,tm in pairs( team.GetAllTeams() ) do
		if tm == teamObj or id == teamObj or tm.Name == teamObj or tm.Color == teamObj or tm.ChatName == teamObj then
			return id
		end
	end
	
	/* Not found ? return false */
	return false
end

/* Get a team object */
team.GetInfo = function( teamObj )
	
	/* Ensure the team exists */
	local teamID = team.Valid( teamObj )
	if teamID == false then
		DEBUG:Msg( "Team not found: " .. tostring( teamObj ), "team.GetInfo" )
		return false
	end
	
	/* Return the team table */
	for id,tm in pairs( team.GetAllTeams() ) do
		if id == teamID then
			return tm
		end
	end
	
	/* Not found ? return false */
	return false
end

/* Get game teams */
team.GetGameTeams = function()
	
	/* Variables */
	local teams = {}
	
	/* Loop teams */
	for id,tm in pairs( team.GetAllTeams() ) do
	
		/* If team is joinable, use it */
		if tm.Joinable then
			table.insert( teams, tm )
			
			/* Debug output */
			DEBUG:Msg( "Game team found: " .. tostring( id ) .. ", " .. tostring( tm.Name ), "team.GetGameTeams" )
		end
	end
	
	/* Return the list of joinable teams */
	return teams
end

/************************************************************************/
/* SERVER ONLY															*/
/************************************************************************/

if SERVER then

/* Query team info */
team.QueryTeamInfo = function( teamObj )

	/* Get the team object */
	local tm = team.GetInfo( teamObj )
	if not tm then
		DEBUG:Msg( "Team not found: " .. tostring( teamObj ), "team.QueryTeamInfo" )
		return false
	end

	/* Associate function to team if needed */
	if not tm.QueryTeamInfo then
		tm.QueryTeamInfo = function() team.QueryTeamInfo( self ) end
	end

	/* Try to load team info */
	return LoadTeamInfo( teamObj )
end

end